const tabs = {
    namespaced: true,
    state: {
        editableTabsValue: '/home',
        editableTabs: [{
            title: '首页',
            name: '/home',
            close: false
        }]
    },
    mutations: {
        // 添加标签页
        add_tabs: (state, tab) => {
            // 判断该标签页是否已存在
            if (state.editableTabs.findIndex(e => e.name === tab.MenuUrl) === -1) {
                state.editableTabs.push({
                    title: tab.MenuName,
                    name: tab.MenuUrl,
                    close: true
                })
            }
            state.editableTabsValue = tab.MenuUrl
        },
        // 切换标签页
        change_tabs: (state, url) => {
            state.editableTabsValue = url
        },
        remove_tab(state,targetName) { // 删除标签页
            let tabs = state.editableTabs;
            let activeName = state.editableTabsValue;
            if (activeName === targetName) {
                tabs.forEach((tab, index) => {
                    if (tab.name === targetName) {
                        let nextTab = tabs[index + 1] || tabs[index - 1];
                        if (nextTab) {
                            activeName = nextTab.name;
                        }
                    }
                });
            }
            state.editableTabsValue = activeName;
            state.editableTabs = tabs.filter(tab => tab.name !== targetName);
        },
    }
}

export default tabs;